namespace LambdaGp.Core.Streaming
{
    public class CompactGenomeLoader : GenomeLoaderBase
    {
        private const string _tokenizer = @"\[.*?]|.";

        public CompactGenomeLoader(GenomeDefinition genomeDefinition)
            : base(genomeDefinition, _tokenizer)
        {
        }

        public CompactGenomeLoader(Population population)
            : this(population.GenomeDefinition)
        {
        }

        protected override void LoadNodeFromTokenQueue(OperatorNode node)
        {
            string token = GetNextToken();

            Operator @operator = GenomeDefinition.GetFunctionByCompactToken(token);

            node.Operator = @operator;

            while (node.Children.Count < @operator.Arity)
            {
                OperatorNode child = new OperatorNode();
                node.Children.Add(child);
                LoadNodeFromTokenQueue(child);
            }            
        }
    }
}
